﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace ParserGenerator.ParserConstruction
{
    [Serializable]
    public sealed class ParserAutomatonContributionMatrix : ReadOnlyDictionary<ParserAutomatonContribution, ParserAutomatonContributionColumn>
    {
        internal ParserAutomatonContributionMatrix()
            : base(new Dictionary<ParserAutomatonContribution, ParserAutomatonContributionColumn>())
        {
        }

        internal void Add(ParserAutomatonContribution contribution, ParserAutomatonContributionColumn column)
        {
            if (contribution == null)
                throw new ArgumentNullException(nameof(contribution));
            // column can be null
            Dictionary.Add(contribution, column);
        }

        internal void AddValueItem(ParserAutomatonContribution contribution, ParserAutomatonItem item)
        {
            if (contribution == null)
                throw new ArgumentNullException(nameof(contribution));
            if (item == null)
                throw new ArgumentNullException(nameof(item));
            ParserAutomatonContributionColumn column;
            if (!Dictionary.TryGetValue(contribution, out column))
            {
                column = new ParserAutomatonContributionColumn();
                Dictionary.Add(contribution, column);
            }
            column.Add(item);
        }
    }
}
